// pages/cart/index.js


import {getSetting,chooseAddress,openSetting,showModal,showToast} from "../../utils/asyncWx";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {request} from '../../request/index1'
Page({
  data:{
    address:{},
    cart:[],
    totalPrice:0,
    totalNum:0
  },
  onShow: function () {
    //1获取缓存中的收货地址信息
    const address = wx.getStorageSync("address");
    let cart= wx.getStorageSync("cart")||[];
    cart= cart.filter(v=>v.checked);
    this.setData({address});

    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v=>{

        totalPrice+=v.num*v.goods_price;
        totalNum+=v.num;
    })
    this.setData({
      cart,
      address,
      totalPrice,
      totalNum
    })
  },
// 点击支付按钮
async handOrderPay(){
  // 获取缓存中有没有token
  const token = wx.getStorageSync("token");
  if(!token){
    wx.navigateTo({
      url: '/pages/auth/index',
    });
      return;
  }
    // console.log("已经存在token");
    // 创建订单，准备请求头2准备请求体参数,3订单地址
    const header= {Authorization:token};
    const order_price=this.data.totalPrice;
    const consignee_addr= this.data.address;
    const cart=this.data.cart;
    let goods=[];
    cart.forEach(v=>goods.push({
      goods_id:v.goods_id,
      goods_number:v.num,
      goods_price:v.goods_price
    }))
    // 准备发送请求，获取订单
    const orderParams={order_price,consignee_addr,goods}
    const res =await request({url:"/my/orders/create",method:"POST",data:orderParams,header:header});
    console.log(res);

}
})